import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    //全排列
    List<List<Integer>> ret;
    List<Integer> path;
    boolean[] check;
    public List<List<Integer>> permuteUnique(int[] nums) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        Arrays.sort(nums);
        check = new boolean[nums.length];
        dfs(nums, 0);
        return ret;
    }
    public void dfs(int[] nums, int index){
        //递归出口
        if(index == nums.length){
            ret.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(check[i] == false && (i == 0 || nums[i] != nums[i - 1] || check[i - 1] != false)){
                path.add(nums[i]);
                check[i] = true;
                dfs(nums, index + 1);
                //回溯
                path.remove(path.size() - 1);
                check[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1, 1, 3};
        solution.permuteUnique(nums);
    }
}